package cn.golxen.rsms.redis;

import cn.golxen.rsms.bean.entity.StoreMain;
import cn.golxen.rsms.bean.entity.StoreOffers;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;

@Service
@Slf4j
public class StoreRedis {
    @Autowired
    RedisTemplate<String, String> redisTemplate;
    //商品活动缓存
    public void addStoreOffers(StoreOffers storeOffers){
        long ttl= storeOffers.getEndDate().toInstant(ZoneOffset.of("+8")).toEpochMilli()/1000-
                LocalDateTime.now().toInstant(ZoneOffset.of("+8")).toEpochMilli()/1000;
        redisTemplate.opsForValue().set("store_offers::" + storeOffers.getStoreId() + "::" + storeOffers.getCode(),
                storeOffers.getAttachStr(),ttl, TimeUnit.SECONDS);
    }
    //全局活动缓存
    public void addStoreOffersGlobal(StoreOffers storeOffers){
        long ttl= storeOffers.getEndDate().toInstant(ZoneOffset.of("+8")).toEpochMilli()/1000-
                LocalDateTime.now().toInstant(ZoneOffset.of("+8")).toEpochMilli()/1000;
        redisTemplate.opsForValue().set("store_offers_global::" + storeOffers.getStoreId() ,
                storeOffers.getAttachStr(),ttl, TimeUnit.SECONDS);
    }


    public String getStoreOffers(String code,int storeId){
        return redisTemplate.opsForValue().get("store_offers::" + storeId + "::" + code);
    }
    public String getStoreOffersGlobal(int storeId){
        return redisTemplate.opsForValue().get("store_offers_global::" + storeId);
    }

    public Boolean delStoreOffers(String code,int storeId){
        return redisTemplate.delete("store_offers::" + storeId + "::" + code);
    }
    public Boolean delStoreGlobalOffers(int storeId){
        return redisTemplate.delete("store_offers_global::" + storeId);
    }
}
